//
//  SettingViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/18.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "SettingViewController.h"
#import "MyTableViewCell.h"
#import "Header.h"
#import "LocaData.h"
#import "UserInfoModal.h"
#import "OptionModal.h"
#import "OptionTableViewCell.h"
#import "LoginViewController.h"
#import "PhotoQualityViewController.h"
#import "FeedbackViewController.h"
#import "AboutMeViewController.h"
#import "AfriendViewController.h"
#import "MBProgressHUD+PKX.h"



@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView         *settingTableView;
@property(nonatomic,strong) NSArray             *optionArray;
@property (nonatomic, assign)BOOL isFirst;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFirst = YES;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:self.isFirst forKey:@"isFirst"];
    });
    // 初始化页面
    [self initView];
    
    // 设置导航控制器
    [self initNavgation];
}

/* - - - - - -- - - - - - - - -页面相关设置 - - - - - - - - - -- -  */

- (void) initView {
    
    self.title = @"设置";
    [self.view setBackgroundColor:Bg_Color];
    
    [self.view addSubview:self.settingTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notific) name:@"updateUser" object:nil];
}

- (void) initNavgation {

    // 设置导航控制器
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
}

/* - - - - - -- - - - - - - - -数据源方法相关设置 - - - - - - - - - -- -  */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }else if (section == 1){
        
        return 1;
    }else {
    
        return [[LocaData readSetOption] count] - 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MY_CELL"];
        
        if (!cell) {
            
            cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MY_CELL"];
        }

        if ([LocaData isLogin] == NO) {
            UserInfoModal *userInfo = [[UserInfoModal alloc] init];

            userInfo.userHead = @"head_icon";
            
            userInfo.userName = @"你还没有登录~";
            
            cell.userInfo = userInfo;
            
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

        } else {
            UserInfoModal *user = [LocaData readUserInfo];
            
            user.userId = user.userId;
            
            user.userHead = user.userHead;
            
            cell.userInfo = user;
        }
        
        [cell setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
        
        return cell;
    }else {
        
        OptionTableViewCell *optionCell = [tableView dequeueReusableCellWithIdentifier:@"Option_Cell"];
        
        if (!optionCell) {
            
            optionCell = [[OptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Option_Cell"];
        }
        
        if (indexPath.section == 1) {
            
            [optionCell setOption:[[LocaData readSetOption] objectAtIndex:indexPath.row]];
            
            [optionCell.lineView setBackgroundColor:[UIColor clearColor]];
            
            [optionCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }else {
            
            [optionCell setOption:[[LocaData readSetOption] objectAtIndex:indexPath.row + 1]];
            
            if (indexPath.row != 3) {
                
                [optionCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            }else {
            
                [optionCell.lineView setBackgroundColor:[UIColor clearColor]];
            }
        }
        [optionCell setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
        
        return optionCell;
    }
}

/* - - - - - -- - - - - - - - -代理方法相关设置 - - - - - - - - - -- -  */

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        
        return My_Height(80);
    }else {
    
        return My_Height(60);
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    switch (section) {
        case 0:
            return @"";
            break;
        case 1:
            return @"设置";
            break;
        case 2:
            return @"关于";
            break;
            
        default:
            break;
    }
    
    return self.title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        if ([LocaData isLogin] == NO) {
            LoginViewController *login = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:login animated:YES];
        }
 
    } else if (indexPath.section == 1) {
        
        PhotoQualityViewController *photo = [[PhotoQualityViewController alloc] init];
        [self.navigationController pushViewController:photo animated:YES];
        
    }else {
        
        switch (indexPath.row) {
            case 0:
                
                [self showFeedBackVC];
                break;
            case 1:
                
                [self showAFriendVC];
                break;
            case 2:
                
                [self showAboutVC];
                break;
            case 3:
                
                if ([LocaData isLogin] == YES) {
                      [self showLoginOut];
                } else {
                    [MBProgressHUD showError:@"您未登录"];
                }
                break;
                
            default:
                break;
        }
    }
}

- (void) showFeedBackVC {

    FeedbackViewController *feedback = [[FeedbackViewController alloc] init];
    [self.navigationController pushViewController:feedback animated:YES];
}

- (void) showAFriendVC {

    AfriendViewController *afeiend = [[AfriendViewController alloc] init];
    [self.navigationController pushViewController:afeiend animated:YES];
}

- (void) showAboutVC {

    AboutMeViewController *about = [[AboutMeViewController alloc] init];
    [self.navigationController pushViewController:about animated:YES];
}

/* - - - - - -- - - - - - - - -点击事件相关设置 - - - - - - - - - -- -  */

- (void) action_backItmeClickEvent {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) notific {
    [self.settingTableView reloadData];

}

- (void) showLoginOut {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出登录？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [LocaData saveUserInfo:[[UserInfoModal alloc] init]];
        
        [self.settingTableView reloadData];
        
        LoginViewController *login = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:login animated:YES];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alert addAction:sureAction];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.settingTableView reloadData];
}


#pragma mark -- 懒加载

- (UITableView *)settingTableView {

    if (!_settingTableView) {
        
        _settingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -My_Height(20), SCREEN_W, SCREEN_H) style:UITableViewStyleGrouped];
        
        [_settingTableView setDataSource:self];
        
        [_settingTableView setBackgroundColor:Bg_Color];
        
        [_settingTableView setDelegate:self];
        
        [_settingTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        
        [_settingTableView setSeparatorColor:[UIColor grayColor]];
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        [_settingTableView setTableHeaderView:headView];
    }
    return _settingTableView;
}

@end
